package com.ytf.core.web.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ytf.core.web.pojo.model.UserRole;
import com.ytf.core.web.mapper.UserRoleMapper;
import com.ytf.core.web.service.IUserRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 * 用户角色关联表 服务实现类
 * </p>
 *
 * @author YTF
 * @since 2025-05-20
 */
@Service
public class UserRoleServiceImpl extends ServiceImpl<UserRoleMapper, UserRole> implements IUserRoleService {

    @Override
    public Boolean removeByUserId(Long userId) {
        LambdaQueryWrapper<UserRole> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserRole::getUserId,userId);
        return this.remove(queryWrapper);
    }

    @Override
    public List<Long> listRoleIdByUserId(Long userId) {
        return lambdaQuery().eq(UserRole::getUserId,userId)
                .select(UserRole::getRoleId).list().stream().map(UserRole::getRoleId).collect(Collectors.toList());
    }

    @Override
    public Boolean removeByUserIdAndRoleId(Long userId, List<Long> roleToRemove) {
        LambdaQueryWrapper<UserRole> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserRole::getUserId,userId).in(UserRole::getRoleId,roleToRemove);
        return this.remove(queryWrapper);
    }
}
